package repo

import (
	"gorm.io/gorm"
	"sensi/app/model"
)

type FirmwareVersion struct {
}

func (FirmwareVersion) List(db *gorm.DB) ([]*model.FirmwareVersion, error) {
	ms := make([]*model.FirmwareVersion, 0)

	err := db.Find(&ms).Error
	return ms, err
}

func (FirmwareVersion) ByID(db *gorm.DB, id int) (*model.FirmwareVersion, error) {
	m := &model.FirmwareVersion{}
	err := db.First(m, id).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return m, err
}

func (FirmwareVersion) ByBroadcast(db *gorm.DB, broadcast string) (*model.FirmwareVersion, error) {
	m := &model.FirmwareVersion{}
	err := db.Where("INSTR(?, broadcast)", broadcast).First(m).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return m, err
}
